﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;

using CNCSoft.EWinform;
using CNCSoft.EWinform.ControlUtils;

using CNCSoft.EWinform.AGrid;
using CNCSoft.EData;

using Common;

namespace CNCSoft.ErpApps.TMS {

	/// <summary>
	/// Summary description for TienMatPhieuThuData.
	/// </summary>
	public class InvoiceData: TMSDataBase  {

		public enum TL  {
            Invoice,
            InvoiceDetail,
			Feeplan,
            FeeplanDetail,
            ChuongTrinhQuanTam,
            BangCap,
            KhoaHoc,
            GioHoc,
            NgayTrongTuan,
            Location
        }

        private string sqlInvoice =
            "SELECT	[InvoiceNumber], [InvoiceDate], [MaDonVi], [EnquiryNumber], [Counsellor], [Curriculum], [Certificate], [MaLop], [StartDate], [TimeSlot], [MaNgayTrongTuan], " +
            "		[Feeplan], [Builling], [Discount], [SoLanDiscount], [Amount], [InvoicePortal], [ReceiptPortal], [BuillingPortal], [DiscountPortal], [AmountPortal], " +
            "        [ThongTinThem], [ChinhSuaFeePlan], [LyDoChinhSuaFeePlan], [SoTienBaoLuu], [NewEnroll], [HinhThucDongTien], [Tag], [MaLocation] " +
			"FROM 	[INVOICE] ";

		private string sqlInvoiceDetail =
            "SELECT [ID], [FeeplanDetailCode], [InvoiceNumber], [Solan],[Ngay], [SoTien], [NoiDung], [SoTienTruocDiscount], [SoTienBaoLuu] " +
			"FROM   [INVOICEDETAIL] ";

		public InvoiceData() {

            ds.Tables.Add("Invoice");
            ds.Tables.Add("InvoiceDetail");
			ds.Tables.Add("Feeplan");
			ds.Tables.Add("FeeplanDetail");
            
            ds.Tables.Add("ChuongTrinhQuanTam");
            ds.Tables.Add("BangCap");
            ds.Tables.Add("KhoaHoc");
            ds.Tables.Add("GioHoc");
            ds.Tables.Add("NgayTrongTuan");
            ds.Tables.Add("Location");

            
			DanhMuc.fill(getTable(TL.Feeplan), DanhMuc.TLDM.Feeplan);
            DanhMuc.fill(getTable(TL.FeeplanDetail), DanhMuc.TLDM.FeeplanDetail);
            DanhMuc.fill(getTable(TL.ChuongTrinhQuanTam), DanhMuc.TLDM.ChuongTrinhQuanTam);
            DanhMuc.fill(getTable(TL.BangCap), DanhMuc.TLDM.BangCap);
            DanhMuc.fill(getTable(TL.KhoaHoc), DanhMuc.TLDM.KhoaHoc);
            DanhMuc.fill(getTable(TL.GioHoc), DanhMuc.TLDM.GioHoc);
            DanhMuc.fill(getTable(TL.NgayTrongTuan), DanhMuc.TLDM.NgayTrongTuan);
            DanhMuc.fill(getTable(TL.Location), DanhMuc.TLDM.Location);
           
			initData("('')", true);
		}

		public void initData(string strInvoiceNumber, bool firstLoading) {
			if (!firstLoading) {
				getTable(TL.InvoiceDetail).Clear();
				getTable(TL.Invoice).Clear();
			}
            SQLHelper.Fill(getTable(TL.Invoice), GlobalVariable.WSInfo.ConnectionString,
                sqlInvoice  + " WHERE [InvoiceNumber] In " + strInvoiceNumber);

            SQLHelper.Fill(getTable(TL.InvoiceDetail), GlobalVariable.WSInfo.ConnectionString,
                sqlInvoiceDetail  + " WHERE InvoiceDetail.InvoiceNumber In " + strInvoiceNumber);

			if (firstLoading) {
				// Set primary key(s)
                getTable(TL.Invoice).PrimaryKey = new DataColumn[] { getTable(TL.Invoice).Columns["InvoiceNumber"] };
				getTable(TL.InvoiceDetail).PrimaryKey = new DataColumn[]{getTable(TL.InvoiceDetail).Columns["ID"]};

				// Set relations
                ds.Relations.Add("M_D", getTable(TL.Invoice).Columns["InvoiceNumber"],
                    getTable(TL.InvoiceDetail).Columns["InvoiceNumber"]);

                // Set default values
                getTable(TL.Invoice).Columns["InvoiceNumber"].DefaultValue = "";
                getTable(TL.Invoice).Columns["InvoiceDate"].DefaultValue = GlobalVariable.WSInfo.WorkingDate;
                getTable(TL.Invoice).Columns["MaDonVi"].DefaultValue = GlobalVariable.WSInfo.CompanyID;
                getTable(TL.Invoice).Columns["StartDate"].DefaultValue = GlobalVariable.WSInfo.WorkingDate;
                getTable(TL.Invoice).Columns["Builling"].DefaultValue = 0;
                getTable(TL.Invoice).Columns["Discount"].DefaultValue = 0;
                getTable(TL.Invoice).Columns["SoLanDiscount"].DefaultValue = 1;
                getTable(TL.Invoice).Columns["Amount"].DefaultValue = 0; 
                getTable(TL.Invoice).Columns["ChinhSuaFeePlan"].DefaultValue = 0;
                getTable(TL.Invoice).Columns["LyDoChinhSuaFeePlan"].DefaultValue = "";
                getTable(TL.Invoice).Columns["Counsellor"].DefaultValue = GlobalVariable.WSInfo.UserID;
                getTable(TL.Invoice).Columns["NewEnroll"].DefaultValue = true;
                getTable(TL.Invoice).Columns["HinhThucDongTien"].DefaultValue = "Cash";

				//Detail
                getTable(TL.InvoiceDetail).Columns["ID"].AutoIncrement = true;
                getTable(TL.InvoiceDetail).Columns["ID"].AutoIncrementStep = 1;
                getTable(TL.InvoiceDetail).Columns["FeeplanDetailCode"].DefaultValue = "";
                getTable(TL.InvoiceDetail).Columns["Ngay"].DefaultValue = GlobalVariable.WSInfo.WorkingDate;
                getTable(TL.InvoiceDetail).Columns["SoTien"].DefaultValue = 0;
                getTable(TL.InvoiceDetail).Columns["NoiDung"].DefaultValue = "";
			}
           
		}

		public bool createNewRow() {
			try {
                
                initData("('')", false);

				DataRow dr = getTable(TL.Invoice).NewRow();
                vg.DateForGenerating = WSInfo.WorkingDate;   //Generate theo ngay dang nhap
                vg.Prefix = "EN";
                vg.VoucherNoFormatDesign = VoucherNoGenerator.VoucherNoFormat.Prefix_YY_MM_Delimiter_SerialNo;
                dr["InvoiceNumber"] = vg.generateVoucherNo("Invoice", "[InvoiceNumber]");
                getTable(TL.Invoice).Rows.Add(dr);

				return true;
			}
			catch {
				MessageBox.Show(RS.ERROR_CREATE_NEW_ROW, RS.ERROR_CREATE_NEW_ROW_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			return false;
		}

		public bool update() {
			try {
                new SimpleMasterDetailUpdater(sqlInvoice, sqlInvoiceDetail, getTable(TL.Invoice), getTable(TL.InvoiceDetail), GlobalVariable.WSInfo.ConnectionString);
                ds.AcceptChanges();
				return true;
			} catch (Exception ex) {
				MessageBox.Show(RS.ERROR_UPDATE +"\n" +ex.Message, RS.ERROR_UPDATE_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			return false;
		}

		public DataTable getTable(TL index) {
			return ds.Tables[(int)index];
		}

	}
}
